package Classes.GameDatas// ActionScript file
{
	import Classes.GameErrors.GameError;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameViews.GV_Map;

	/**
	 *地图数据类 
	 * @author 葛增文
	 * 
	 */	
	public class GD_Map extends GameData
	{
		private var _widthNum:int;/*地图宽度*/
		private var _heightNum:int;/*地图高度*/
		private var _zombiesObj:Object;/*僵尸存储器*/
		private var _buildingsObj:Object;/*建筑存储器*/
		private var _sundriesObj:Object;/*杂物存储器*/
		private var _decorationsObj:Object;/*装饰物存储器*/
		private var _sickMansObj:Object;/*病人存储器*/
		private var _wallsAry:Array;/*围墙数组*/
		private var _isShow:Boolean;/*显示状态*/
		private var _mainBuilding:GD_Building_Func;/*避难所主建筑*/
		public function GD_Map():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._zombiesObj=new Object;
			this._sundriesObj=new Object;
			this._buildingsObj=new Object;
			this._decorationsObj=new Object;
			this._sickMansObj=new Object;
			_wallsAry=new Array;
		}
		
		
		/**
		 *创建一个地图对象 
		 * @param creatObj 创建数据对象
		 * @return 地图对象
		 * 
		 */		
		public static function creat(wallsAry:Array,buildingsAry:Array,zombiesAry:Array,sickMansAry:Array,sundriesAry:Array,widthNum:int=200,heightNum:int=200):GD_Map
		{
			var mapData:GD_Map=new GD_Map();
			mapData._widthNum=widthNum;
			mapData._heightNum=heightNum;
			mapData.creatBuildings(buildingsAry);
			mapData.creatSundries(sundriesAry);
			mapData.creatWalls(wallsAry);
			mapData.creatZombies(zombiesAry);
			mapData.creatSickMans(sickMansAry);
			return mapData;
		}
		
		/**
		 *  拓展围墙 进行刷新  或者  中心建筑 升级导致房屋升级
		 * 
		 */		
		public  function refreshWalls(newWallsArr:Array,isUpMainBuild:Boolean=false):void
		{
			GV_Map.instance.removeOldHavenView(wallsAry);
			_wallsAry=[];
			creatWalls(newWallsArr,isUpMainBuild);
			GV_Map.instance.creatHaven(GD_Player.instance.wallLv);
			GV_Map.instance.creatWalls(wallsAry);

			GD_HavenWall.zombieToNoHaven();		//僵尸移出围墙内
			GD_HavenWall.setWallsVisible();		//
		}
		
		public function creatWalls(wallDatasAry:Array,isUpMainBuild:Boolean=false):void
		{
			for(var i:int=0;i<wallDatasAry.length;i++)
			{
				var wallData:Object=wallDatasAry[i];
				var gd_havenWall:GD_HavenWall;
				if(isUpMainBuild)
				{
					gd_havenWall=GD_HavenWall.creat({wallSerial:wallData.wallSerial,type:(this.havenLv-1),dur:(this.havenLv+2)});
				}else{
					gd_havenWall=GD_HavenWall.creat({wallSerial:wallData.wallSerial,type:(this.havenLv-1),dur:wallData.dur});
				}
				
				wallsAry.push(gd_havenWall);
			}
		}
		
		private function creatBuildings(buildingDatasAry:Array):void
		{
			for each(var buildingObj:Object in buildingDatasAry)
			{
				this.addBuilding(buildingObj);
			}
		}
		
		/**
		 * 刷僵尸 ，当僵尸少于一定数目时候 
		 * @param zombieDatasAry
		 */		
		public function addZombies(zombieDatasAry:Array):void
		{
			var tmpAddZombiesObj:Object=new Object();
			for each(var zombieObj:Object in zombieDatasAry)
			{
				var gd_zombie:GD_Zombie=this.addZombie(zombieObj);
				tmpAddZombiesObj[gd_zombie.serverID]=gd_zombie;
			}
			GV_Map.instance.creatZombies(tmpAddZombiesObj);
		}
		
		private function creatZombies(zombieDatasAry:Array):void
		{
			for each(var zombieObj:Object in zombieDatasAry)
			{
				this.addZombie(zombieObj);
			}
		}
		
		private function creatSickMans(sickManDatasAry:Array):void
		{
			for each(var sickManObj:Object in sickManDatasAry)
			{
				this.addSickMan(sickManObj);
			}
		}
		
		/**
		 * 增加 获得杂物、垃圾 
		 * @param sundryDatasAry
		 */			
		public function addSundries(sundryDatasAry:Array):void
		{
			var tmpAddSundriesObj:Object=new Object();
			for each(var sundriesObj:Object in sundryDatasAry)
			{
				var gd_sundry:GD_Building_Sundry=this.addSundry(sundriesObj);
				tmpAddSundriesObj[gd_sundry.serverID]=gd_sundry;
				//new Guide
				if(gd_sundry.zt_other_type==30000023)
				{
					GU_dealPower.instance.addArrowToZW(gd_sundry.serverID);	
				}
			}
			GV_Map.instance.creatSundries(tmpAddSundriesObj);
		}
		
		private function creatSundries(sundryDatasAry:Array):void
		{
			for each(var sundryObj:Object in sundryDatasAry)
			{
				this.addSundry(sundryObj);
			}
		}
		
		public function addBuilding(buildingObj:Object):GD_Building_Func
		{
			var gd_building:GD_Building_Func=GD_Building_Func.creat(buildingObj,this);
			if(this.buildingsObj.hasOwnProperty(gd_building.serverID))throw new GameError("建筑物已存在");
			this.buildingsObj[gd_building.serverID]=gd_building;
			if(gd_building.buildingType==40101001)this._mainBuilding=gd_building;
			//if(this.isShow)GV_Map.instance.creatBuilding(gd_building);
			return gd_building;
		}
		
		public function addSundry(sundryObj:Object):GD_Building_Sundry
		{
			var gd_sundry:GD_Building_Sundry=GD_Building_Sundry.creat(sundryObj,this);
			if(this.sundriesObj.hasOwnProperty(gd_sundry.serverID))throw new GameError("杂物已存在");
			this.sundriesObj[gd_sundry.serverID]=gd_sundry;
			return gd_sundry;
		}
		
		public function addZombie(zombieObj:Object):GD_Zombie
		{
			var gd_zombie:GD_Zombie=GD_Zombie.creat(zombieObj,this);
			if(this.zombiesObj.hasOwnProperty(gd_zombie.serverID))throw new GameError("僵尸已存在");
			this.zombiesObj[gd_zombie.serverID]=gd_zombie;
			return gd_zombie;
		}
		
		public function addSickMan(sickManObj:Object):GD_SickMan
		{
			var gd_sickMan:GD_SickMan=GD_SickMan.creat(sickManObj,this);
			if(this.sickMansObj.hasOwnProperty(gd_sickMan.serverID))throw new GameError("病人已存在");
			this.sickMansObj[gd_sickMan.serverID]=gd_sickMan;
			//if(this.isShow)GV_Map.instance.creatSickMan(gd_sickMan);
			return gd_sickMan;
		}
		
		public function getBuildingByID(buildingID:Number):GD_Building_Func
		{
			if(!this.buildingsObj.hasOwnProperty(buildingID))throw new GameError("建筑物不存在");
			return this.buildingsObj[buildingID] as GD_Building_Func;
		}
		
		public function hasBulidingByFunId(buildingID:Number,isAddStoreDecide:Boolean=false):Boolean
		{
			var result:Boolean=false;
			var tmpb:Boolean=false;
			var tmpg:Boolean=GD_Player.instance.hasbuildInStroeByFunid(buildingID)==0?false:true;
			for each(var tb:GD_Building_Func in this.buildingsObj)
			{
				if(buildingID==tb.buildingType)
				{
					tmpb=true;
					break;
				}
			}
			
			if(isAddStoreDecide)
			{
				if(!tmpb && !tmpg)
				{
					result=false;
				}else{
					result=true;
				}
			}else{
				result=tmpb;
			}
			
			return result;
		}
		
		public function getZombieByID(zombieID:Number):GD_Zombie
		{
			if(!this.zombiesObj.hasOwnProperty(zombieID))throw new GameError("僵尸不存在");
			return this.zombiesObj[zombieID] as GD_Zombie;
		}
		
		public function getSickManByID(sickManID:Number):GD_SickMan
		{
			if(!this.sickMansObj.hasOwnProperty(sickManID))throw new GameError("病人不存在");
			return this.sickMansObj[sickManID] as GD_SickMan;
		}
		
		public function removeBuildingByID(buildingID:Number):void
		{
			delete this.buildingsObj[buildingID];
		}
		
		public function removeZombieByID(zombieID:Number):void
		{
			delete this.zombiesObj[zombieID];
		}
		
		public function removeSickManByID(sickManID:Number):void
		{
			delete this.sickMansObj[sickManID];
		}
		
		public function creatMap():void
		{
			GV_Map.instance.creat(this);
			this.isShow=true;
		}
		
		/**
		 *替换临时建筑ID 
		 * @param tmpID 建筑临时ID
		 * @param serverID 赋予的实际ID
		 * 
		 */		
		public function replaceBuildingID(tmpID:Number,serverID:Number):void
		{
			var replaceBuilding:GD_Building_Func=this.buildingsObj[tmpID];
			if(replaceBuilding)
			{
				replaceBuilding.serverID=serverID;
				delete this.buildingsObj[tmpID];
				this.buildingsObj[replaceBuilding.serverID]=replaceBuilding;
			}
		}

		/*getter & setter*/
		public  function get wallsAry():Array
		{
			return _wallsAry;
		}

		public function get zombiesObj():Object
		{
			return _zombiesObj;
		}

		public function get buildingsObj():Object
		{
			return _buildingsObj;
		}

		public function get decorationsObj():Object
		{
			return _decorationsObj;
		}

		public function get sickMansObj():Object
		{
			return _sickMansObj;
		}

		public function get widthNum():int
		{
			return _widthNum;
		}

		public function get heightNum():int
		{
			return _heightNum;
		}

		public function get isShow():Boolean
		{
			return _isShow;
		}

		public function set isShow(value:Boolean):void
		{
			_isShow = value;
		}

		public function get havenLv():int
		{
			return this.mainBuilding.buildingLv;
		}

		public function get mainBuilding():GD_Building_Func
		{
			return _mainBuilding;
		}

		public function get sundriesObj():Object
		{
			return _sundriesObj;
		}

		/**
		 * 去好友家时  释放到 GD_Map的数据 
		 */		
		public function gotoFriendHomeReleaseData():void
		{
			this.isShow=false;
			for each(var gb:GD_Building_Func in this.buildingsObj)
			{
				gb.release();
			}
			this._buildingsObj=null;
			for each(var gbsundry:GD_Building_Sundry in this.sundriesObj)
			{
				gbsundry.release();
			}
			this._sundriesObj=null;
			for each(var gbz:GD_Zombie in this.zombiesObj)
			{
				gbz.release();
			}
			this._zombiesObj=null;
			this._decorationsObj=null;
			for each(var sickMan:GD_SickMan in this.sickMansObj)
			{
				sickMan.release();
			}
			this._sickMansObj=null;
			this._wallsAry=null;
		}

	}
}